package com.bkgy.jobs.outputL3Listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bkgy.controller.tpdol3data.entity.TPdoDataL3Cal1630;
import com.bkgy.controller.tpdol3data.service.ITPdoL3Cal1630Service;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.dao.IConstMapper;
import com.bkgy.sys.common.entity.SysConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WatchCal1630PdoL3Service {
    @Resource
    private List<ICal1630L3Listener> listeners;
    @Autowired
    ITPdoL3Cal1630Service service;
    @Autowired
    IConstMapper constMapper;
    boolean flag = true;

    public void exec() {
        if (!flag)
            return;
        flag = false;
        LambdaQueryWrapper<TPdoDataL3Cal1630> wrapper = new LambdaQueryWrapper<>();
        String topId = CommonConstant.constMap.get(CommonConstant.CAL1630l3_REC_ID);

        wrapper.gt(TPdoDataL3Cal1630::getIndocno, topId).orderByDesc(TPdoDataL3Cal1630::getIndocno);
        List<TPdoDataL3Cal1630> list = service.list(wrapper);
        if (null != list && list.size() > 0) {
            //通知订阅服务者处理
            for (ICal1630L3Listener l : listeners) {
                l.hasNew1630Record(list);
            }

            //更新最新记录编号
            topId = list.get(0).getIndocno() + "";
            constMapper.updateById(new SysConst(CommonConstant.CAL1630l3_REC_ID, topId, null));
            CommonConstant.constMap.put(CommonConstant.CAL1630l3_REC_ID, topId);
        }
        flag = true;
    }
}
